package com.zhuangjie.shop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.CommonSeparatorConstants;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.shop.dto.DishPageWrapperPageDTO;
import com.zhuangjie.shop.dto.DishSaveDTO;
import com.zhuangjie.shop.entity.Dish;
import com.zhuangjie.shop.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品表 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-09-24
 */
@RestController
@RequestMapping("/dish")
@Api(tags = "店铺菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询接口
     *
     * @param shopId             店铺id
     * @param dishPageWrapperDTO 碟形纸包装dto
     * @return {@link R}
     * @throws Exception 例外
     */
    @GetMapping("/queryShopDishListPage")
    @ApiOperation("菜品分页查询接口")
    @NoAuthentication
    public R queryShopDishListPage(@RequestParam(required = false) Long shopId, @Valid DishPageWrapperPageDTO dishPageWrapperDTO) throws Exception {
        Page<Dish> page = dishService.queryShopCategoryListPage(shopId,dishPageWrapperDTO);
        // 获取分页查询的数据
        List<Dish> dishList = page.getRecords();
        // 前端要完成分页，需要返回下面两个数据
        return R.ok().dataAsMap(m->{
            m.put("listData",dishList);
            m.put("total",page.getTotal());
        });
    }

    /**
     * 更改菜品状态
     *
     * @return {@link R}
     */
    @PostMapping("/changeDishStatus")
    @ApiOperation("更改菜品状态")
    public R changeDishStatus(@NotNull(message = "菜品id不能为空！") @RequestParam("dishId") Long dishId,@NotNull(message = "状态不能为空！") @RequestParam("status") Integer status) {
        return R.capture(()->dishService.changeDishStatus(dishId,status));
    }


    /**
     * 批量删除删除根据ids
     *
     * @param dishIds dish id
     * @return {@link R}
     */
    @PostMapping("/deleteBatchDishes")
    @ApiOperation("根据菜品id删除菜品")
    public R deleteBatchDishes(@RequestBody List<String> dishIds) {
        return R.capture(()->dishService.deleteBatchDishes(dishIds));
    }

    @PutMapping("/saveShopDish")
    @ApiOperation("添加店铺菜品")
    public R saveShopDish(@Valid @RequestBody DishSaveDTO dishSaveDTO) {
        return R.capture(()->dishService.saveShopDish(dishSaveDTO));
    }

    @GetMapping("/getShopDishInfo/{dishId}")
    @ApiOperation("获取菜品数据")
        public R getShopDishInfo(@PathVariable("dishId") Long dishId) {
        DishSaveDTO dishSaveDTO = dishService.getShopDishInfoById(dishId);
        return R.require(dishSaveDTO);
    }
    @PostMapping("/changeBetterDisplayStatus")
    @ApiOperation("修改菜品优先显示的状态")
    public R changeBetterDisplayStatus(@NotNull(message = "传入的菜品id不能为空") @RequestParam("dishId") Long dishId,
                                       @NotNull(message = "新的优先展示状态不能为空") @RequestParam("betterDisplayStatus") Integer betterDisplayStatus) {

        return R.capture(()->dishService.changeBetterDisplayStatus(dishId,betterDisplayStatus));
    }

    @GetMapping("/dish-name")
    @ApiOperation("根据菜品id查询菜品名称")
    public R queryDishNames(@RequestParam("dishIds") @NotNull(message = "菜品分类id不能为空！") String dishIds) {
        List<Long> dishIdList = Arrays.stream(dishIds.split(CommonSeparatorConstants.MULTIPLE_SEPARATOR)).map(Long::valueOf).collect(Collectors.toList());
        return R.ok().data(dishService.queryDishCategoryNameByIds(dishIdList));
    }

}

